home *** CD-ROM | disk | FTP | other *** search
/ CD Classic 39 / CD CLASSIC #39 (1998).iso / EMPRESA / visio / Vistdstd / Install / Data.Z / Visreg.BAS < prev    next >
BASIC Source File  |  1996-08-13  |  15KB  |  441 lines

  1. Attribute VB_Name = "VISREG"
  2. ' VISREG.BAS - Visio Instance Registration Utilites
  3. ' Copyright (C) 1991-1996 Visio Corporation. All rights reserved.
  4. '
  5. '
  6. ' Abstract      Contains helper functions for working with Visio instances.
  7. '               To use this module include it into your project and use one
  8. '               of the three levels available.  For more information see
  9. '               below.
  10. '
  11. '               The registration utility offers an easy way get and create
  12. '               Visio instance objects.  It offers three levels of instancing
  13. '               from simple get/create/release to registration where the
  14. '               library maintains the "signature" of a Visio instance and
  15. '               warns you when the active instance changes.
  16. '
  17. '               The library maintains a static global g_appVisio which
  18. '               is called the global instance object (GIO).  Use GIO in
  19. '               your code when refering to the working instance of Visio.
  20. '               Never apply the Set operator to GIO yourself (unless you
  21. '               really know what your doing).
  22. '
  23. '               To use the library, include it in your project and refer the
  24. '               visio application object through g_appVisio (GIO).
  25. '               Read the sections below to find the level of functionality
  26. '               you want.
  27. '
  28. '               Low Level Routines
  29. '
  30. '               The low level routines are almost identical to what you
  31. '               would use normally with GetObject and CreateObject.
  32. '               However they encapsulate the error handling.
  33. '
  34. '                   vaoGetGIO()     Retrieves active, running instances.
  35. '                   vaoCreateGIO()  Creates a new instance.
  36. '                   vaoReleaseGIO() Release the GIO instance if Set.
  37. '                   vaoIsGIOValid() Verifies that GIO is Set and loaded.
  38. '
  39. '               Registration/Release Level
  40. '
  41. '                   Use this level when you need the registration functions
  42. '                   to maintain the GIO but want control over how it is
  43. '                   obtained.  The procedures are:
  44. '
  45. '                       vaoRegisterGIO()
  46. '                       vaoUnRegistrGIO()
  47. '                       vaoReSetGIO()
  48. '                       (vaoReleaseGIO()    [From low level])
  49. '
  50. '                   To begin you register your instance and choose how to
  51. '                   retrieve the GIO (Get/Create/Both) with
  52. '                   vaoRegisterGIO() and use vaoUnRegisterGIO to release
  53. '                   it.  This gives a good amount of flexibility but leaves
  54. '                   it up to you to handle the conditions where Visio is shut
  55. '                   down or a new instance is loaded.  At this level you keep
  56. '                   the instance registered but release the GIO using
  57. '                   vaoReleaseGIO.  To get back GIO use vaoReSetGIO.
  58. '
  59. '               Most Common Level
  60. '
  61. '                   This is highest, most abstract level.  It's called Most
  62. '                   Common level because most scripts will probably use it
  63. '                   to get instance objects.  There is one function:
  64. '
  65. '                       vaoGetObject()
  66. '
  67. '                   When called it will check to see if the GIO is already
  68. '                   registered.  If not it will first attempt a GetObject
  69. '                   and, if that fails, will use CreateObject.  Unless Visio
  70. '                   is not installed, you will get visOK back.  On subsequent
  71. '                   calls it checks that it is still valid (not UnSet and
  72. '                   still running).  If so it returns visOK, otherwise it
  73. '                   tries to register the GIO again.  If that fails you
  74. '                   receive visError.  The nice thing about this is that one
  75. '                   call maintains the GIO for you.
  76. '
  77.  
  78. Option Explicit                                 '-- All Variable Explicit!
  79.  
  80. ' Declare the global Application object using the type library reference.
  81. ' Change this to Global g_appVisio As Object if you choose not to use the
  82. ' type library.
  83. Global g_appVisio As Visio.Application
  84.  
  85. Const REG_GET_HWND = 1
  86. Const REG_SET_HWND = 2
  87.  
  88. Global Const visDiffInst = 1
  89. Global Const visGet = 2
  90. Global Const visCreate = 3
  91. Global Const visVisioQuit = 4
  92. Global Const visError = 5
  93. Global Const visRegistered = 6
  94. Global Const visOK = 7
  95.  
  96. Private Function GetHWND()
  97. '-------------------------------
  98. '--- GetHWND -------------------
  99. '--
  100. '--   Returns the registered Visio Window Handle.
  101. '--
  102.  
  103.     Dim iTemp As Integer
  104.  
  105.     VisWindowHandle REG_GET_HWND, iTemp
  106.  
  107.     GetHWND = iTemp
  108. End Function
  109.  
  110. Private Function Registered() As Integer
  111. '-------------------------------
  112. '--- RegisterVisio -------------
  113. '--
  114. '--   Returns boolean integer indicating if we are registered or not.
  115. '--
  116.  
  117.     Registered = (GetHWND() <> 0)
  118. End Function
  119.  
  120. Private Sub SetHWND(ByVal iNewHWND As Integer)
  121. '-------------------------------
  122. '--- SetHWND -------------------
  123. '--
  124. '--   Sets the registered Visio Window Handle.
  125. '--
  126.  
  127.     VisWindowHandle REG_SET_HWND, iNewHWND
  128. End Sub
  129.  
  130. Function vaoCreateGIO() As Integer
  131. '-------------------------------
  132. '--- vaoCreateGIO --------------
  133. '--
  134. '--   Uses CreateObject to create a new instance of Visio.  If it fails
  135. '-- False is returned, otherwise the GIO is set to the instance created
  136. '-- and True is returned.
  137. '--
  138.  
  139.     On Error GoTo vaoCreateGIOErrorHandler
  140.  
  141.     Debug.Print "VISREG.BAS vaoCreateGIO() - Creating new Visio instance."
  142.  
  143.     Set g_appVisio = CreateObject("visio.application")
  144.  
  145.     If Not (g_appVisio Is Nothing) Then
  146.         vaoCreateGIO = True
  147.     End If
  148.  
  149.     Exit Function
  150.  
  151. vaoCreateGIOErrorHandler:
  152.     Debug.Print "VISREG.BAS vaoCreateGIO() - Failed."
  153.     Exit Function
  154.     Resume Next
  155. End Function
  156.  
  157. Function vaoGetGIO() As Integer
  158. '-------------------------------
  159. '--- vaoGetGIO -----------------
  160. '--
  161. '--   Uses GetObject to get the active instance of Visio.  If GetObject fails
  162. '-- False is returned, otherwise the GIO is set and True is returned.
  163. '--
  164.  
  165.     On Error GoTo vaoGetErrorHandler
  166.  
  167.     Debug.Print "VISREG.BAS vaoGetGIO() - Retrieving active Visio instance."
  168.  
  169.     Set g_appVisio = GetObject(, "visio.application")
  170.  
  171.     If Not (g_appVisio Is Nothing) Then
  172.         vaoGetGIO = True
  173.     End If
  174.     
  175.     Exit Function
  176.  
  177. vaoGetErrorHandler:
  178.     Debug.Print "VISREG.BAS vaoGetGIO() - Failed."
  179.     Exit Function
  180.     Resume Next
  181. End Function
  182.  
  183. Function vaoGetObject() As Integer
  184. '-------------------------------
  185. '--- vaoGetObject --------------
  186. '--
  187. '--   Uses registration procedures to maintain the GIO.  This funciton makes
  188. '-- up the Common Use Layer (most commonly used procedure) for using the GIO.
  189. '-- Just call it every time you need to work with Visio and it will make sure
  190. '-- you have a valid working copy.
  191. '--
  192. '-- Return Values:
  193. '--  visOK        - The GIO is set to a valid working instance of Visio.
  194. '--  visError     - Visio or OLE not installed or some other serious
  195. '--                 error occurred.
  196. '--
  197.  
  198.     Dim iRetVal As Integer, iTemp As Integer, l_appVisio As Object
  199.     
  200.     iRetVal = visOK                             '-- Default To OK
  201.  
  202.     If Registered() Then                        '-- When Registerd...
  203.         If Not vaoIsGIOValid() Then             '--   If GIO Is Valid...
  204.             Debug.Print "VISREG.BAS vaoGetObject() - Re-registering instance."
  205.  
  206.             '-- Somehow the GIO is no longer valid, either because it was
  207.             '-- vaoReleaseGIO'd or is no longer running.  Therefore we just
  208.             '-- try to re-register and if the same instance is active, we
  209.             '-- get that one again.  Otherwise we end up with the active
  210.             '-- instance of Visio or a newly created one.
  211.             '--
  212.             '-- In future versions of Visio we will iterate through the
  213.             '-- instance collection and retrieve the instance we originally
  214.             '-- registered to if it still exists.
  215.  
  216.             vaoUnRegisterGIO                    '--   Oops, Its Bad Now...
  217.  
  218.             If vaoRegisterGIO(True, True) = visError Then
  219.                 iRetVal = visError
  220.             End If
  221.         End If
  222.     Else
  223.         If vaoRegisterGIO(True, True) = visError Then
  224.             iRetVal = visError
  225.         End If
  226.     End If
  227.  
  228.     vaoGetObject = iRetVal
  229. End Function
  230.  
  231. Function vaoGetVisio(bGet As Integer, bCreate As Integer) As Integer
  232. '-------------------------------
  233. '--- vaoGetVisio ---------------
  234. '--
  235. '--   Identical to vaoRegisterGIO except doesn't use registration functions.
  236. '--
  237. '-- Parameters : bUseExisting - Boolean - Use vaoGetGIO() first.
  238. '--              bCreate      - Boolean - Use vaoCreateGIO().
  239. '--
  240. '--    Returns : visError     - If an error occurred and the GIO could not be
  241. '--                             set.  Either the flags were invalid or
  242. '--                             Get & Create failed.
  243. '--              visGet       - When a vaoGetGIO() retrieved the GIO.
  244. '--              visCreate    - When a vaoCreateGIO() retrieved the GIO.
  245. '--              visRegisterd - Failed - GIO is registered.  Use
  246. '--                             vaoUnRegisterGIO().
  247. '--
  248.  
  249.     Dim iRetVal As Integer
  250.  
  251.     ' If registered we fail.
  252.     '
  253.     If Registered() Then
  254.         iRetVal = visRegistered
  255.         GoTo lblGetVisioCleanUp
  256.     End If
  257.  
  258.     iRetVal = visError
  259.     
  260.     ' If the Get flag was set we first try vaoGetGIO()
  261.     '
  262.     If bGet Then
  263.         If vaoGetGIO() Then iRetVal = visGet
  264.     End If
  265.  
  266.     ' If the Create flag is on and the return value doesn't indicate that
  267.     ' a get worked then we use create.
  268.     '
  269.     If bCreate And (iRetVal <> visGet) Then
  270.         If vaoCreateGIO() Then iRetVal = visCreate
  271.     End If
  272.  
  273.     ' If the GIO isn't set at this point we output an error message.
  274.     '
  275.     If g_appVisio Is Nothing Then
  276.         Debug.Print "VISREG.BAS vaoGetVisio() - Error registering GIO."
  277.     End If
  278.                 
  279. lblGetVisioCleanUp:
  280.     vaoGetVisio = iRetVal
  281. End Function
  282.  
  283. Function vaoIsGIOValid() As Integer
  284. '-------------------------------
  285. '--- vaoIsGIOValid -------------
  286. '--
  287. '--   Our validity test simply checks to see if the GIO is set and, if so,
  288. '-- checks if it is loaded.
  289. '--
  290. '-- Returns : True if GIO is set and loaded, False otherwise.
  291. '--
  292.     On Error GoTo lblvaoGIOValidErr
  293.  
  294.     Dim iTemp As Integer
  295.  
  296.     vaoIsGIOValid = False                           '-- Default To False
  297.  
  298.     If (g_appVisio Is Nothing) Then Exit Function   '-- Not Set
  299.  
  300.     iTemp = g_appVisio.Documents.Count              '-- Try A Property
  301.  
  302.     vaoIsGIOValid = True                            '-- No Error - Valid!
  303.     Exit Function
  304.  
  305. lblvaoGIOValidErr:
  306.     Exit Function                                   '-- Error - Invalid
  307.     Resume Next
  308. End Function
  309.  
  310. Function vaoRegisterGIO(bUseExisting As Integer, bCreate As Integer) As Integer
  311. '-------------------------------
  312. '--- vaoRegisterGIO ------------
  313. '--
  314. '--   Registers the GIO using two parameters to decide how the Visio instance
  315. '-- should be created.  Use vaoUnRegisterGIO to reverse the registration.
  316. '--
  317. '-- Parameters : bUseExisting - If True then GetObject will tried first.
  318. '--              bCreate      - If True CreateObject will be called after
  319. '--                             any GetObject calls.
  320. '--
  321. '--    Returns : visError     - If an error occurred and the GIO could not be
  322. '--                             registered because either the flags passed
  323. '--                             were invalid or Get & Create failed.
  324. '--              visGet       - When a GetObject retrieved the GIO.
  325. '--              visCreate    - When a CreateObject retrieved the GIO.
  326. '--              visRegisterd - When already registered.
  327. '--
  328.  
  329.     Dim iRetVal As Integer
  330.  
  331.     If Registered() Then
  332.         iRetVal = visRegistered
  333.         GoTo lblRegisterCleanUp
  334.     End If
  335.  
  336.     iRetVal = visError
  337.  
  338.     If bUseExisting Then
  339.         If vaoGetGIO() Then iRetVal = visGet
  340.     End If
  341.  
  342.     If bCreate And (iRetVal <> visGet) Then
  343.         If vaoCreateGIO() Then iRetVal = visCreate
  344.     End If
  345.  
  346.     If g_appVisio Is Nothing Then
  347.         Debug.Print "VISREG.BAS vaoRegisterGIO() - Error registering GIO."
  348.     Else
  349.         SetHWND g_appVisio.WindowHandle
  350.     End If
  351.  
  352. lblRegisterCleanUp:
  353.     vaoRegisterGIO = iRetVal
  354. End Function
  355.  
  356. Sub vaoReleaseGIO()
  357. '-------------------------------
  358. '--- vaoReleaseGIO -------------
  359. '--
  360. '--   Handles releasing the GIO.  Does not unregister the window handle.
  361. '-- If using the registration interfaces use vaoReSetGIO to retrieve the
  362. '-- GIO, otherwise you may use vaoGetGIO or vaoCreateGIO.  This does not
  363. '-- take affect until all other references go out of scope.
  364. '--
  365.  
  366.     Set g_appVisio = Nothing                '-- Release Resources
  367.  
  368.     Debug.Print "VISREG.BAS vaoReleaseGIO() - Complete."
  369. End Sub
  370.  
  371. Function vaoReSetGIO() As Integer
  372. '-------------------------------
  373. '--- vaoReSetGIO ---------------
  374. '--
  375. '--   Tries to re-Set the GIO only if we are registered and the GIO is not
  376. '-- already set.
  377. '--
  378. '-- Return Values :
  379. '--   visError     - If not registered or GIO is already set.
  380. '--   visOK        - If able to reSet the GIO to the registered instance.
  381. '--   visVisioQuit - If Visio is no longer running.  If so then the GIO is
  382. '--                  unregistered because the HWND is no longer valid.
  383. '--   visDiffInst  - If the registered instance is no longer running.  The
  384. '--                  GIO is not set.
  385. '--
  386.  
  387.     vaoReSetGIO = visError
  388.     
  389.     If Not Registered() Or Not (g_appVisio Is Nothing) Then Exit Function
  390.  
  391.     If vaoGetGIO() Then
  392.         If g_appVisio.WindowHandle = GetHWND() Then
  393.             vaoReSetGIO = visOK
  394.         Else
  395.             vaoReleaseGIO
  396.             vaoReSetGIO = visDiffInst       '-- Release GIO
  397.         End If
  398.     Else
  399.         vaoReSetGIO = visVisioQuit
  400.         vaoUnRegisterGIO                    '-- UnRegister
  401.     End If
  402. End Function
  403.  
  404. Sub vaoUnRegisterGIO()
  405. '-------------------------------
  406. '--- vaoUnRegisterGIO ----------
  407. '--
  408. '--   Unregisters a visio instance by clearing the window handle and releasing
  409. '-- the global instance object.
  410. '--
  411.     
  412.     SetHWND 0                           '-- Resets HWND
  413.     vaoReleaseGIO                       '-- Releases GIO
  414.     
  415.     Debug.Print "VISREG.BAS vaoUnRegisterGIO() - Completed."
  416. End Sub
  417.  
  418. Private Sub VisWindowHandle(ByVal iAction As Integer, iArg As Integer)
  419. '-------------------------------
  420. '--- VisWindowHandle -----------
  421. '--
  422. '--   Maintains the registered window handle in a static variable.
  423. '--
  424. '-- Parameters : iAction - Specifies the action to perform.  REG_GET_HWND
  425. '--                        sets iArg to the handle.  REG_SET_HWND sets the
  426. '--                        handle to iArg.
  427. '--
  428. '--              iArg    - Used in gets/sets.
  429. '--
  430.  
  431.     Static iHWND As Integer
  432.  
  433.     Select Case iAction
  434.         Case REG_GET_HWND: iArg = iHWND
  435.         Case REG_SET_HWND: iHWND = iArg
  436.         Case Else:
  437.             Debug.Print "VISREG.BAS VisWindowHandle() - Invalid Action Passed"
  438.     End Select
  439. End Sub
  440.  
  441.